package com.kyzh.core.activities

import android.os.Bundle
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.module.LoadMoreModule
import com.chad.library.adapter.base.viewholder.BaseDataBindingHolder
import com.gushenge.core.base.activity.BaseActivity
import com.gushenge.core.beans.MyOrder
import com.gushenge.core.requests.WealRequest
import com.kyzh.core.R
import com.kyzh.core.databinding.ItemMyorderBinding
import com.kyzh.core.utils.copy
import com.gushenge.core.toast
import kotlinx.android.synthetic.main.activity_myorder.*

class MyOrderActivity : BaseActivity() {

    private val beans = arrayListOf<MyOrder>()
    private val adapter = Adapter(R.layout.item_myorder,beans)
    private var p =1
    private var max =2
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_myorder)
        initView()
    }

    private fun initView() {
        rvList.adapter = adapter
        refresh()
        adapter.loadMoreModule.isEnableLoadMore=true
        adapter.loadMoreModule.isEnableLoadMoreIfNotFullPage=false
        adapter.loadMoreModule.setOnLoadMoreListener {
            if (max<p){
                adapter.loadMoreModule.loadMoreEnd()
            }else{
                getData()
                adapter.loadMoreModule.loadMoreComplete()
            }
        }
        root.setOnRefreshListener {
            refresh()
            root.isRefreshing = false
        }
    }


    private inner class Adapter(layout:Int,beans:ArrayList<MyOrder>):BaseQuickAdapter<MyOrder,BaseDataBindingHolder<ItemMyorderBinding>>(layout,beans), LoadMoreModule {
        override fun convert(holder: BaseDataBindingHolder<ItemMyorderBinding>, item: MyOrder) {
            holder.dataBinding?.data = item
            holder.itemView.setOnClickListener {
                if (item.log_sn.isEmpty()){
                    getString(R.string.noExpressInfo).toast()
                }else{
                    item.log_sn.copy()
                    "快递单号已复制".toast()
                }
            }
        }

    }



    fun getData(){
        WealRequest.myOrder(p){ p, max_p, data->
            this.p = p
            this.max = max_p
            beans.addAll(data)
            adapter.notifyDataSetChanged()
        }
    }
    fun refresh(){
        WealRequest.myOrder(1){ p, max_p, data->
            this.p = p
            this.max = max_p
            beans.clear()
            beans.addAll(data)
            adapter.notifyDataSetChanged()
        }
    }
}